﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace MyConsoleApp.Code
{
    /*
     public class a
    {
        [JsonProperty]
        public string name { get; set; }

        [JsonIgnore]
        public string title { get; set; }

        public IList<b> blist { get; set; }
        public c cObj { get; set; }
    }

    public class c
    {
        public string title { get; set; }
        public IList<b> blist { get; set; }
    }

    public class b
    {
        public string name { get; set; }
    }
     * static void Main(string[] args)
        {
            JsonHelper jh = new JsonHelper();

            var b1 = new b { name = "b1" };
            var b2 = new b { name = "b2" };

            var c = new c();
            c.blist = new List<b> { b1 };

            var a = new a();
            a.name = "123";
            a.cObj = c;
            a.blist = new List<b> { b1, b2 };

            var s = jh.SerializeObject(a);
            Console.WriteLine(s);
            var aa = jh.Deserialize<a>(s);

            Console.ReadLine();
        }
     */
    public class JsonHelper
    {
        public T Deserialize<T>(string jstring) where T : class
        {
            T obj = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jstring);
            return obj;
        }

        public string SerializeObject(object o, JsonSerializerSettings setting = null)
        {
            string json = string.Empty;
            if (setting == null)
            {
                json = JsonConvert.SerializeObject(o);
            }
            else
            {
                json = JsonConvert.SerializeObject(o, setting);
            }
            
            return json;
        }
    }
}
